package com.huaruan.youdi.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.google.gson.Gson;
import com.huaruan.youdi.eventbus.EventBus;
import com.huaruan.youdi.eventbus.EventNames;
import com.huaruan.youdi.views.activitys.MainActivity;

import cn.jpush.android.api.JPushInterface;

/**
 * 自定义JPush接收器
 * <p>
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class JReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Bundle bundle = intent.getExtras();
            EventBus.getInstance().notifiDataUpdate(EventNames.REFRESH_MAIN, "");
            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
                //极光服务器分配的Registration Id，
                String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                //自定义消息

            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                //推送通知

            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                String json = bundle.getString(JPushInterface.EXTRA_EXTRA);

                Gson gson = new Gson();
//                JPushModel model = gson.fromJson(json, JPushModel.class);
//                if ("0".equals(model.type)) {
//                    Intent i = new Intent(context, AddFriendsActivity.class);
//                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    context.startActivity(i);
//                } else if ("1".equals(model.type)) {
//                    Bundle b = new Bundle();
//                    b.putString("toAccount", model.fromAccount);
//                    b.putString("toUserName", model.fromUserName);
//                    b.putString("toUserImageUrl", model.fromUserImageUrl);
//                    Intent i = new Intent(context, ChatActivity.class);
//                    i.putExtras(b);
//                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    context.startActivity(i);
//                } else if ("2".equals(model.type)) {
//                    Intent i = new Intent(context, MyGetPraiseActivity.class);
//                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    context.startActivity(i);
//                } else if ("3".equals(model.type)) {
//                    Intent i = new Intent(context, MyCommentActivity.class);
//                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    context.startActivity(i);
//                } else if ("4".equals(model.type)) {
//                } else {
//                    Intent i = new Intent(context, MainActivity.class);
//                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    context.startActivity(i);
//                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
